<template>
	<view class="container">
		<view class="print">
			<template v-if="State==''">
				<view class="printname" >{{BluetoothName}}</view>
			</template>
			<template v-if="State=='已连接'">
				<view class="printname">{{BluetoothName}}<text class="green">（{{State}}）</text></view>
				<view class="printbtn blue" @click="CancelConn()">断开连接</view>
			</template>
			<template v-if="State=='未连接'">
				<view class="printname red" v-if="State=='未连接'">{{BluetoothName}}</view>
				<view class="printbtn blue" @click="ReConn()">重新连接</view>
			</template>
		</view>
		<view class="square">
			<view class="title">{{ComName}}</view>
			<view class="title">责令（限期）改正通知书</view>
			<view class="subtitle">{{Info.DocNum}}</view>
			<view class="line">{{Info.RealName}}：</view>
			<view class="line indent" v-if="EasyZeGai1!=''">{{EasyZeGai1}}</view>
			<view class="line indent" v-if="EasyZeGai2!=''">{{EasyZeGai2}}</view>
			<view class="line indent" v-if="EasyZeGai3!=''">{{EasyZeGai3}}</view>
			<view class="line indent">行政机关联系人: {{Info.OfficeRealName}}</view>
			<view class="line indent" style="margin-top:-5rpx;">行政机关电话: {{Info.OfficeMobile}}</view>
			<view class="line indent" style="margin-top:-5rpx;">行政机关地址: {{Info.OfficeAddress}}</view>
			<view class="line indent">当事人签名: </view>
			<view class="imgview">
				<view class="img1"><image :src="Tools.GetImg(Info.SignName,500)"/></view>
				<view class="img2"><image :src="Chapter"/></view>
			</view>
			<view class="scan"><view class="txt1"></view><view class="txt2">{{Info.CreateDate.ToDateTwo()}}</view></view>
		</view>
		<canvas style="width: 560px; height: 240px; position: absolute; left:-1000px; bottom:-1000px;" canvas-id="chapter"></canvas>
		<view class="btn">
			<button class="publishs" @click="Print" v-if="IsReady && State=='已连接'">打印</button>
			<button class="publishs graybg" v-else>请稍候...</button>
		</view>
	</view>
</template>

<script>
	import Bluetooth from '@/utils/bluetooth.js'
	var esc = require("@/utils/esc.js");
	export default {
		data() {
			return {
				ID:0,
				Info:{ID:0},
				SignNameData:'',
				ChapterData:'',
				Contact:{},
				Chapter:'',
				ComName:'',
				EasyZeGai1:'',
				EasyZeGai2:'',
				EasyZeGai3:'',
				State:'',
				IsReady:false,
				BluetoothName:'正在搜索蓝牙打印机...'
			};
		},
		onLoad(options) {
			this.ID=options.ID;
			uni.getBluetoothAdapterState({
			  success:(res)=> {
				  this.IsConn();
			  },fail:(res)=> {
			  	this.Init();
			  }
			});
			uni.onBLEConnectionStateChange(res=> {
			  if(!res.connected){
				  this.State="未连接";
				  this.BluetoothName="请打开手机蓝牙和蓝牙打印机";
			  }
			});
			this.Find_EasyInfo();
		},
		methods:{
			Init(){
				Bluetooth.InitBluetooth(list=>{
					console.log("链接的设备",list);
					if(list.length>0){
						this.State="已连接";
						this.BluetoothName="H96蓝牙打印机";
					}else{
						this.State="未连接";
						this.BluetoothName="请打开手机蓝牙和蓝牙打印机";
					}
				});
			},
			InitDrivers(){
				Bluetooth.GetDevices(list=>{
					if(list.length>0){
						this.State="已连接";
						this.BluetoothName="H96蓝牙打印机";
					}else{
						this.State="未连接";
						this.BluetoothName="请打开手机蓝牙和蓝牙打印机";
					}
				});
			},
			IsConn(){
				uni.getBLEDeviceRSSI({
				  deviceId:uni.getStorageSync("deviceId"),
				  success:json=> {
					this.State="已连接";
					this.BluetoothName="H96蓝牙打印机";
				  },
				  fail:res=>{
					  Bluetooth.Connect(item=>{
						if(item){
							this.State="已连接";
							this.BluetoothName="H96蓝牙打印机";
						}else{
							this.State="未连接";
							this.BluetoothName="请打开手机蓝牙和蓝牙打印机";
							this.InitDrivers();
						}
					  });
						
				  }
				});
			},
			ReConn(){
				this.State='';
				this.BluetoothName="正在搜索蓝牙打印机...";
				this.IsConn();
			},
			CancelConn(){
				this.Util.Confirm("您确定要断开连接吗？",item=>{
					Bluetooth.CloseBLEConnection(item=>{
						this.State="未连接";
					});
				});
			},
			Find_EasyInfo(){
				var data = {};
				data.ID=this.ID;
				data.Act="Find_EasyInfo";
				this.Util.Loading("加载中...");
				this.Util.Post(data, json=>{
					this.Util.HideLoading();
					uni.stopPullDownRefresh();
					uni.hideNavigationBarLoading();
					this.Info = json.Data.Info;
					this.Contact = json.Data.Contact; 
					this.Chapter = json.Data.Chapter;
					this.ComName=json.Data.ComName;
					this.EasyZeGai1 = json.Data.EasyZeGai1;
					this.EasyZeGai2 = json.Data.EasyZeGai2;
					this.EasyZeGai3 = json.Data.EasyZeGai3;
					this.Util.InitSignNameChapter_Easy_H96(this.Info.SignName.GetImg(500),this.Chapter,()=>{
						setTimeout(()=>{
							this.GetChapterData();
						},1000);
					});			
					
				});
			},
			Print(){
				if(this.State!="已连接"){
					this.Util.Toast("请先连接打印机");
					return;
				}
				this.Util.Loading("打印中...");
				var command = esc.jpPrinter.createNew();
				command.init();
				command.setPrintAndFeedRow(1);
				command.setSelectJustification(1); //居中显示
				command.setText(this.ComName);
				command.setText("责令（限期）改正通知书");
				command.setPrintAndFeedRow(1);
				command.setText(this.Info.DocNum);
				command.setPrintAndFeedRow(1);
				command.setSelectJustification(0); //居左显示
				command.setText(this.Info.RealName+":");
				if(this.EasyZeGai1!=""){
					command.setText("    " + this.EasyZeGai1);
					command.setPrintAndFeedRow(1);
				}
				if(this.EasyZeGai2!=""){
					command.setText("    " + this.EasyZeGai2);
					command.setPrintAndFeedRow(1);
				}
				if(this.EasyZeGai3!=""){
					command.setText("    " + this.EasyZeGai3);
					command.setPrintAndFeedRow(1);
				}
				command.rowSpace(15);
				command.setText("    行政机关联系人: " + this.Info.OfficeRealName)
				command.setText("    行政机关电话: " + this.Info.OfficeMobile)
				command.setText("    行政机关地址: " + this.Info.OfficeAddress)
				command.setPrintAndFeedRow(1);
				command.setText("    当事人签名: ")
				command.setPrintAndFeedRow(1);
				command.setSelectJustification(2); //居右显示
				command.setPrintAndFeedRow(1);
				command.setBitmap(this.ChapterData);
				command.init();
				command.setSelectJustification(0); //居左显示
				command.setText("                                  " + this.Info.CreateDate.ToDateTwo());
				command.setPrintAndFeedRow(5);
				command.SendBlData(command.getData());
				this.Util.HideLoading();
			},
			GetChapterData(){
				uni.canvasGetImageData({
				  canvasId: 'chapter',
				  x: 0,
				  y: 0,
				  width: 560,
				  height: 240,
				  success : res => {
					  this.IsReady = true;
					  this.ChapterData = res;
				  }
				});
			}
		}
	}
</script>

<style lang="scss">
	.container{width:100%; padding-top:30rpx; padding-bottom:140rpx;
		.print{width:100%; height:90rpx; line-height: 80rpx; font-size:32rpx; padding-left:3%; display: flex; flex-direction: row;
			.printname{flex:1;}
			.printbtn{width:150rpx;}
		}
		.square{width:94%; margin-left:3%; height:auto; border-radius: 10rpx; background-color: #ffffff; padding:40rpx; 
			.title{width:100%; height:70rpx; line-height: 70rpx; font-size:40rpx; text-align: center;}
			.subtitle{width:100%; height:60rpx; line-height: 60rpx; font-size:30rpx; text-align: center;}
			.line{width:100%; height:auto; min-height: 50rpx; line-height:50rpx; margin-top:20rpx; font-size:32rpx; color:#696969;}
			.imgview{display: flex; flex-direction: row; justify-content: space-between; margin-top:20rpx;
				.img1{width:240rpx; height:120rpx; margin-top:10rpx; float:left; image{width:100%; height:100%;}}
				.img2{width:240rpx; height:240rpx; float:left; image{width:100%; height:100%;}}
			}
			.scan{width:100%; height:32rpx; font-size:32rpx; padding-top:10rpx; display: flex; flex-direction: row; justify-content: space-between;
				.txt1{width:240rpx; text-align: center;}
				.txt2{width:240rpx; text-align: center;}
			}
		}
	}
</style>
